package com.zz.nondatain.view.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityAboutBinding;

import java.util.Calendar;

/**
 * 关于我们页面
 */
public class AboutActivity extends BaseActivity<ActivityAboutBinding> {

    private static final String PRIVACY_POLICY_URL = "http://fjy.handongchina.com:9010/neutral-sjtjz-policy.html";
    private static final String SERVICE_PHONE = "028-12344321";
    private static final String COMPANY_WEBSITE = "www.baidu.com";
    private static final String COMPANY_NAME = "达达科技有限公司";
    
    private boolean isWebViewActive = false;

    @Override
    protected void initView() {
        // 设置返回按钮点击事件
        binding.ivBack.setOnClickListener(v -> {
            if (isWebViewActive) {
                hideWebView();
            } else {
                finish();
            }
        });
        
        // 设置网页关闭按钮点击事件
        binding.ivWebClose.setOnClickListener(v -> hideWebView());
        
        // 设置服务电话点击事件
        binding.tvServicePhone.setOnClickListener(v -> callServicePhone());
        
        // 设置公司官网点击事件
        binding.tvCompanyWebsite.setOnClickListener(v -> openWebsite());
        
        // 设置隐私协议点击事件
        binding.tvPrivacyPolicy.setOnClickListener(v -> openPrivacyPolicy());
        
        // 初始化WebView
        setupWebView();
    }
    
    /**
     * 设置WebView的属性
     */
    private void setupWebView() {
        WebView webView = binding.webView;
        
        // 配置WebView设置
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 启用JavaScript
        webSettings.setDomStorageEnabled(true); // 启用DOM存储
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // 设置缓存模式
        webSettings.setUseWideViewPort(true); // 使用宽视图
        webSettings.setLoadWithOverviewMode(true); // 加载时以概览模式
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setBuiltInZoomControls(true); // 内置缩放控件
        webSettings.setDisplayZoomControls(false); // 不显示缩放控件
        
        // 设置WebViewClient以在应用内打开链接
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
            
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // 网页加载完成后显示WebView
                if (binding.webView.getVisibility() == View.GONE) {
                    showWebView();
                }
            }
        });
        
//        // 设置WebChromeClient以获取网页标题
//        webView.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//                // 更新标题
//                binding.tvTitle.setText(title);
//            }
//        });
    }

    @Override
    protected void initData() {
        // 设置版本信息
        String versionName = getVersionName();
        binding.tvVersion.setText("v" + versionName);
        
        // 设置服务电话
        binding.tvServicePhone.setText("服务电话：" + SERVICE_PHONE);
        
        // 设置公司名称
        binding.tvCompanyName.setText("公司名称：" + COMPANY_NAME);
        
        // 设置公司官网
        binding.tvCompanyWebsite.setText("公司官网：" + COMPANY_WEBSITE);
        
        // 设置版权信息年份
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        binding.tvCopyrightYear.setText("Copyright©2007-" + currentYear);
    }

    @Override
    protected void initObservers() {
        // 无需观察者
    }

    @Override
    protected ActivityAboutBinding getViewBinding() {
        return ActivityAboutBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 获取应用版本名
     */
    private String getVersionName() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "1.0.0"; // 默认版本号
        }
    }
    
    /**
     * 拨打服务电话
     */
    private void callServicePhone() {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + SERVICE_PHONE));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            showToast("无法拨打电话");
        }
    }
    
    /**
     * 打开公司官网
     */
    private void openWebsite() {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            // 确保网址包含http或https前缀
            String url = COMPANY_WEBSITE;
            if (!url.startsWith("http://") && !url.startsWith("https://")) {
                url = "http://" + url;
            }
            intent.setData(Uri.parse(url));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            showToast("无法打开网站");
        }
    }
    
    /**
     * 打开隐私政策
     */
    private void openPrivacyPolicy() {
        try {
            // 更新标题
            binding.tvTitle.setText("隐私协议");
            
            // 在WebView中加载URL
            binding.webView.loadUrl(PRIVACY_POLICY_URL);
        } catch (Exception e) {
            e.printStackTrace();
            showToast("无法打开隐私协议");
        }
    }
    
    /**
     * 显示WebView并隐藏内容
     */
    private void showWebView() {
        binding.webView.setVisibility(View.VISIBLE);
        binding.scrollContent.setVisibility(View.GONE);
        binding.ivWebClose.setVisibility(View.VISIBLE);
        isWebViewActive = true;
    }
    
    /**
     * 隐藏WebView并显示内容
     */
    private void hideWebView() {
        binding.webView.setVisibility(View.GONE);
        binding.scrollContent.setVisibility(View.VISIBLE);
        binding.ivWebClose.setVisibility(View.GONE);
        binding.tvTitle.setText("关于我们");
        isWebViewActive = false;
    }
    
    @Override
    public void onBackPressed() {
        if (isWebViewActive) {
            // 如果WebView正在显示，按返回键时先隐藏WebView
            hideWebView();
        } else {
            super.onBackPressed();
        }
    }
} 